package com.yycx.module.bus.provider.handler.custombond;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  市场保证金列表
 *
 * @author zyf
 */
@Component("customBondPageListHandler")
public class CustomBondPageListHandler implements PageInterceptor<BusPayment> {
    @Autowired
    private TaskService taskService;

    @Autowired
    private BaseUserService userService;

    @Autowired
    private BusOrderDetailsService orderDetailsService;

    @Autowired
    private BusPaymentService paymentService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        Long companyId = OpenHelper.getCompanyId();
        Integer paymentState=params.getInt("paymentState");
        String orderNo=params.get("orderNo");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        cq.select(BusPayment.class, "*");
        cq.joinSelect(BusDept.class, "deptName");
        cq.addSelect(BaseUser.class, "nickName");
        cq.addSelect("deptSaleMan.saleManName");
        cq.eq(true, "payment.companyId", companyId);
        cq.eq("payment.paymentNo", orderNo);
        cq.eq(BusSupplier.class,"supplierId");
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge(BusOrder.class, "sendDate", beginDate);
        }
        if (FlymeUtils.isNotEmpty(endDate)) {
            cq.le(BusOrder.class, "sendDate", endDate);
        }
        if(paymentState.equals(0)){
            cq.in("payment.paymentState", 0,1,2);
        }
        if(paymentState.equals(1)){
            cq.in("payment.paymentState", 0,1);
        }
        if(paymentState.equals(2)){
            cq.eq("payment.paymentState", 2);
        }
        cq.orderByDesc("payment.createTime");
        cq.createJoin(BusSaleman.class).setMainAlias("dept").setMainField("saleManId").setJoinAlias("deptSaleMan").setJoinField("saleManId");
        cq.eq(true, "amountType", AmountTypeEnum.CustomBond.getCode());
        cq.createJoin(BaseUser.class);
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPayment> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            Long orderId = entityMap.getLong("orderId");
            Long paymentId = entityMap.getLong("paymentId");
            Integer paymentState = entityMap.getInt("paymentState");
            Long payUserId = entityMap.getLong("payUserId");
            if(FlymeUtils.isNotEmpty(payUserId)){
                BaseUser payUser=userService.getById(payUserId);
                entityMap.put("payUserName",payUser.getNickName());
            }
            if (FlymeUtils.isNotEmpty(processInstanceId) && paymentState.equals(1)) {
                //查询当前审批节点
                Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                if (FlymeUtils.isNotEmpty(task)) {
                    entityMap.put("taskName", task.getName());
                    entityMap.put("taskId", task.getId());
                    entityMap.put("taskDefinitionKey", task.getTaskDefinitionKey());
                }
            }
        }
        return result;
    }
}
